﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using P2PWeb.Models;
using P2PWeb.Common;
using Nectar.DAL.Common;
using System.Data.Objects.DataClasses;
using System.ComponentModel.DataAnnotations;
using System.Reflection;


namespace P2PWeb.Controllers
{
    public class AccountInfoObject
    {
        [Required(ErrorMessage = "请选择房产情况")]
        [Range(1, 3, ErrorMessage = "请选择房产情况")]
        public int Property { get; set; }

        public global::System.Int32 ID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择性别")]
        [Range(1, 2, ErrorMessage = "请选择性别")]
        public Nullable<global::System.Int32> Sex
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写年龄")]
        [Range(1, 200, ErrorMessage = "年龄选择范围为1到200岁")]
        public Nullable<global::System.Int32> Age
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择最高学历")]
        [Range(1, 7, ErrorMessage = "请选择最高学历")]
        public global::System.Int32? Education
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择婚姻状况")]
        [Range(1, 5, ErrorMessage = "请选择婚姻状况")]
        public global::System.Int32? Marital
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择所在省")]
        [Range(1, 9999, ErrorMessage = "请选择所在省")]
        public Nullable<global::System.Int32> ProvincialID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择所在县")]
        [Range(1, 9999, ErrorMessage = "请选择所在县")]
        public Nullable<global::System.Int32> CityID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择所在市")]
        [Range(1, 9999, ErrorMessage = "请选择所在市")]
        public Nullable<global::System.Int32> CountyID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写户籍地址")]
        public global::System.String AreaName
        {
            get;
            set;
        }

        [RegularExpression("^[0-9]*$", ErrorMessage = "信用卡最高额度只能是整数数字")]
        public Nullable<global::System.Int32> CreditQuota
        {
            get;
            set;
        }

        public AccountInfo GetAccount()
        {
            return new AccountInfo()
            {
                Age = this.Age,
                AreaName = this.AreaName,
                CityID = this.CityID,
                CountyID = this.CountyID,
                CreditQuota = this.CreditQuota,
                Education = this.Education,
                ID = this.ID,
                Marital = this.Marital,
                Property = this.Property,
                ProvincialID = this.ProvincialID,
                Sex = this.Sex,
            };
        }
    }

    public class ContactInfoObject
    {
        public global::System.Int32 ID
        {
            get;
            set;
        }



        [Required(ErrorMessage = "请填写家庭联系人姓名")]
        public global::System.String FamilyContact
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择联系人与本人的关系")]
        [Range(1, 4, ErrorMessage = "请选择联系人与本人的关系")]
        public Nullable<global::System.Int32> FamilyRelation
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写联系人的电话号码或手机号码")]
        public global::System.String FamilyPhone
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写工作联系人姓名")]
        public global::System.String WorkContact
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择联系人与本人的关系")]
        [Range(1, 4, ErrorMessage = "请选择联系人与本人的关系")]
        public Nullable<global::System.Int32> WorkRelation
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写联系人的电话号码或手机号码")]
        public global::System.String WorkPhone
        {
            get;
            set;
        }


        [Required(ErrorMessage = "其他联系人姓名")]
        public global::System.String OtherContact
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择联系人与本人的关系")]
        [Range(1, 4, ErrorMessage = "请选择联系人与本人的关系")]
        public Nullable<global::System.Int32> OtherRelation
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写联系人的电话号码或手机号码")]
        public global::System.String OtherPhone
        {
            get;
            set;
        }

        public ContactInfo GetContactInfo()
        {
            return new ContactInfo()
            {
                FamilyContact = this.FamilyContact,
                FamilyPhone = this.FamilyPhone,
                FamilyRelation = this.FamilyRelation,
                ID = this.ID,
                OtherContact = this.OtherContact,
                OtherPhone = this.OtherPhone,
                OtherRelation = this.OtherRelation,
                WorkContact = this.WorkContact,
                WorkPhone = this.WorkPhone,
                WorkRelation = this.WorkRelation,
            };
        }
    }

    public class WorkInfoObject
    {
        public global::System.Int32 ID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写工作单位全称")]
        public global::System.String CompanyName
        {
            get;
            set;
        }


        [Required(ErrorMessage = "请选择单位所在省")]
        [Range(1, 9999, ErrorMessage = "请选择单位所在省")]
        public global::System.Int32? ProvincialID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择单位所在市")]
        [Range(1, 9999, ErrorMessage = "请选择单位所在市")]
        public global::System.Int32? CityID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择单位所在县")]
        [Range(1, 9999, ErrorMessage = "请选择单位所在县")]
        public global::System.Int32? CountyID
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写单位地址")]
        public global::System.String AreaName
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写区号")]
        [RegularExpression("^[0-9]*$", ErrorMessage = "区号必须是数字")]
        public string PhoneNumber { get; set; }

        [Required(ErrorMessage = "请填写电话号码")]
        [RegularExpression("^[0-9]*$", ErrorMessage = "电话号码必须是数字")]
        public string PhoneInAreaNumber { get; set; }

        [RegularExpression("^[0-9]*$", ErrorMessage = "分机号必须是数字")]
        public string PhoneExtention { get; set; }


        [Required(ErrorMessage = "请选择入职日期")]
        [DataType(DataType.Date, ErrorMessage = "时间格式为 yyyy-mm-dd(yyyy为年份，mm为月份，dd为天数)")]
        public Nullable<global::System.DateTime> WorkDate
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写所在部门")]
        public global::System.String Department
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请填写担任职务")]
        public global::System.String Job
        {
            get;
            set;
        }

        [Required(ErrorMessage = "请选择公司性质")]
        [Range(1, 4, ErrorMessage = "请选择单位所在县")]
        public Nullable<global::System.Int32> CompanyType
        {
            get;
            set;
        }

        public WorkInfo GetWorkInfo()
        {
            return new WorkInfo()
            {
                AreaName = this.AreaName,
                CityID = this.CityID,
                CompanyName = this.CompanyName,
                CompanyType = this.CompanyType,
                CountyID = this.CountyID,
                Department = this.Department,
                ID = this.ID,
                Job = this.Job,
                Phone=this.PhoneInAreaNumber+"-"+this.PhoneNumber+"-"+this.PhoneExtention,
                ProvincialID = this.ProvincialID,
                WorkDate = this.WorkDate,
            };
        }
    }

    public class AccountInfoDetail
    {
        public AccountInfoDetail()
        {

        }

        public AccountInfoDetail(Account account)
        {
            if (account.AccountInfo != null)
            {
                AccountInfo = new AccountInfoObject()
                {
                    Age = account.AccountInfo.Age,
                    AreaName = account.AccountInfo.AreaName,
                    CityID = account.AccountInfo.CityID,
                    CountyID = account.AccountInfo.CountyID,
                    CreditQuota = account.AccountInfo.CreditQuota,
                    Education = account.AccountInfo.Education,
                    ID = account.AccountInfo.ID,
                    Marital = account.AccountInfo.Marital,
                    ProvincialID = account.AccountInfo.ProvincialID,
                    Sex = account.AccountInfo.Sex,
                    Property=account.AccountInfo.Property.GetValueOrDefault(),
                };
            }
            else
            {
                AccountInfo = new AccountInfoObject()
                {
                    Age = null,
                    AreaName = null,
                    CityID = -1,
                    CountyID = -1,
                    CreditQuota = null,
                    Education = -1,
                    ID = -1,
                    Marital = -1,
                    ProvincialID = -1,
                    Sex = -1,
                };
            }

            if (account.ContactInfo != null)
            {
                ContactInfo = new ContactInfoObject()
                {
                    FamilyContact = account.ContactInfo.FamilyContact,
                    FamilyPhone = account.ContactInfo.FamilyPhone,
                    FamilyRelation = account.ContactInfo.FamilyRelation,
                    ID = account.ContactInfo.ID,
                    OtherContact = account.ContactInfo.OtherContact,
                    OtherPhone = account.ContactInfo.OtherPhone,
                    OtherRelation = account.ContactInfo.OtherRelation,
                    WorkContact = account.ContactInfo.WorkContact,
                    WorkPhone = account.ContactInfo.WorkPhone,
                    WorkRelation = account.ContactInfo.WorkRelation,
                };
            }
            else
            {
                ContactInfo = new ContactInfoObject()
                {
                    FamilyContact = null,
                    FamilyPhone = null,
                    FamilyRelation = -1,
                    ID = -1,
                    OtherContact = null,
                    OtherPhone = null,
                    OtherRelation = -1,
                    WorkContact = null,
                    WorkPhone = null,
                    WorkRelation = -1,
                };
            }
            if (account.WorkInfo != null)
            {
                string[] workPhone = account.WorkInfo.Phone.Split(new[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
                WorkInfo = new WorkInfoObject()
                {
                    AreaName = account.WorkInfo.AreaName,
                    CityID = account.WorkInfo.CityID,
                    CompanyName = account.WorkInfo.CompanyName,
                    CompanyType = account.WorkInfo.CompanyType,
                    CountyID = account.WorkInfo.CountyID,
                    Department = account.WorkInfo.Department,
                    ID = account.WorkInfo.ID,
                    Job = account.WorkInfo.Job,
                    ProvincialID = account.WorkInfo.ProvincialID,
                    WorkDate = account.WorkInfo.WorkDate,
                   
                };
                switch (workPhone.Count())
                {
                    case 1:
                        WorkInfo.PhoneInAreaNumber = workPhone[0];
                        break;
                    case 2:
                        WorkInfo.PhoneInAreaNumber = workPhone[0];
                        WorkInfo.PhoneNumber = workPhone[1];
                        break;
                    case 3:
                        WorkInfo.PhoneInAreaNumber = workPhone[0];
                        WorkInfo.PhoneNumber = workPhone[1];
                        WorkInfo.PhoneExtention = workPhone[2];
                        break;
                    default:
                        break;
                }
            }
            else
            {
                WorkInfo = new WorkInfoObject()
                {
                    AreaName = null,
                    CityID = -1,
                    CompanyName = null,
                    CompanyType = -1,
                    CountyID = -1,
                    Department = null,
                    ID = -1,
                    Job = null,
                    ProvincialID = -1,
                    WorkDate = null,
                };
            }
        }

        public AccountInfoObject AccountInfo { get; set; }

        public ContactInfoObject ContactInfo { get; set; }

        public WorkInfoObject WorkInfo { get; set; }
    }

    [AccountAuthorize(AccountType.Admin, AccountType.LookThrough, AccountType.User, AccountType.Agent)]
    public class AccountController : BaseController
    {
        //
        // GET: /Account/

        public ActionResult Index()
        {
            using (var context = new Repository<P2PModelContainer>())
            {
                Account curAccount = context.GetEntityByID<Account>(CurrentUserInfo.ID, new List<string>(
                    new[] 
                    {
                        "AccountAuthentication" ,
                        "AccountInfo",
                        "ContactInfo",
                        "WorkInfo"
                    }));
                return View(curAccount);
            }
        }


        public ActionResult AccountUploadify()
        {
            using (var context = new Repository<P2PModelContainer>())
            {
                Account curAccount = context.GetEntityByID<Account>(CurrentUserInfo.ID, new List<string>(new[] { "AccountAuthentication" }));
                if (curAccount.AccountAuthentication == null)
                    curAccount.AccountAuthentication = new AccountAuthentication();
                return View(curAccount);
            }
        }

        [ValidateAntiForgeryToken]
        public ActionResult SubmitUploadify(AccountAuthentication accountAuthentication)
        {

            using (var context = new Repository<P2PModelContainer>())
            {
                Account curAccount = context.GetEntityByID<Account>(CurrentUserInfo.ID);

                
                
                if (accountAuthentication.IDCardUrl == null && accountAuthentication.IDCardUrlNegative == null)
                {
                    return new ContentResult()
                    {
                        Content = string.Format("<script>alert(\"缺少身份证信息\");window.location='{0}'</script>", Request.UrlReferrer.ToString())
                    };
                }
                if (curAccount.AccountAuthentication != null)
                {
                    context.Delete<AccountAuthentication>(curAccount.AccountAuthentication);
                }
                curAccount.AccountAuthentication = context.Add<AccountAuthentication>(accountAuthentication);
                curAccount.AccountAuthentication.Status = (int)AccountAuthenticationState.Finish;
                curAccount.Status = (int)AccountState.LookThrough;
                context.Update(curAccount);
            }
            return RedirectToAction("Index", "Account");
        }



        

        public ActionResult AccountInfo()
        {
            using (var context = new Repository<P2PModelContainer>())
            {
                Account account = context.GetEntityByID<Account>(CurrentUserInfo.ID, new List<string>() 
                {
                    "AccountInfo",
                    "ContactInfo",
                    "WorkInfo"
                });
                return View(new AccountInfoDetail(account));
            }
        }

        public ActionResult UpdateAccountInfo(AccountInfoDetail accountInfoDetail)
        {
            if (ModelState.IsValid)
            {
                using (var context = new Repository<P2PModelContainer>())
                {
                    Account account = context.GetEntityByID<Account>(CurrentUserInfo.ID, new List<string>() 
                    {
                        "AccountInfo",
                        "ContactInfo",
                        "WorkInfo",
                        "AccountAuthentication"
                    });
                    if (account.AccountInfo == null)
                    {
                        account.AccountInfo = context.Add<AccountInfo>(accountInfoDetail.AccountInfo.GetAccount());
                    }
                    else
                    {
                        account.AccountInfo = context.modify<AccountInfo>(account.AccountInfo, accountInfoDetail.AccountInfo.GetAccount());
                    }

                    if (account.WorkInfo == null)
                    {
                        account.WorkInfo = context.Add<WorkInfo>(accountInfoDetail.WorkInfo.GetWorkInfo());
                    }
                    else
                    {
                        account.WorkInfo = context.modify<WorkInfo>(account.WorkInfo, accountInfoDetail.WorkInfo.GetWorkInfo());
                    }


                    if (account.ContactInfo == null)
                    {
                        account.ContactInfo = context.Add<ContactInfo>(accountInfoDetail.ContactInfo.GetContactInfo());
                    }
                    else
                    {
                        account.ContactInfo = context.modify<ContactInfo>(account.ContactInfo, accountInfoDetail.ContactInfo.GetContactInfo());
                    }

                    if (account.AccountAuthentication != null && account.AccountAuthentication.Status == (int)AccountAuthenticationState.Finish)
                    {
                        account.Status = (int)AccountState.LookThrough;
                    }
                    context.Update<Account>(account);

                }
                return RedirectToAction("index");
            }
            return View(accountInfoDetail);
        }
    }
}